
package com.yimixian.app.goods;

import com.yimixian.app.R;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.GoodDetail;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class GoodsItemView extends FrameLayout {

    @InjectView(R.id.iv_good_img)
    ImageView mIvGoodImg;
    @InjectView(R.id.tv_fruit_name)
    TextView mTvFruitName;
    @InjectView(R.id.tv_fruit_num)
    TextView mTvFruitNum;
    @InjectView(R.id.iv_cart_item)
    ImageView mIvCartItem;
    @InjectView(R.id.tv_good_item_price)
    TextView mTvGoodItemPrice;
    @InjectView(R.id.iv_top1)
    ImageView mIvTop1;
    @InjectView(R.id.rl_buy)
    RelativeLayout mRlBuy;
    @InjectView(R.id.tv_soldout)
    TextView mTvSoldout;
    private GoodDetail.BestPartner mGoodsItem;
    private IDetailGoodItemSelect mButtonListener;
    private DataManager mDataManager;
    private int mScreenWidth;

    public GoodsItemView(Context context) {
        super(context);
        initGoodsView();
    }

    public GoodsItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initGoodsView();
    }

    public GoodsItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initGoodsView();
    }

    private void initGoodsView() {
        LayoutInflater.from(getContext()).inflate(R.layout.detail_item_good_view, this);
        mDataManager = DataManager.getInstance();
        ButterKnife.inject(this);

        Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        mScreenWidth = size.x;
        mIvCartItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mButtonListener != null) {
                    mButtonListener.onGoodItemSelect();
                }
            }
        });
    }

    public void bind(final GoodDetail.BestPartner goodsItem) {
        if (goodsItem == null) {
            return;
        }
        mGoodsItem = goodsItem;
        // mGoodsImage.setImageRatio(mGoodsItem.imgRatio);
        /*
         * Picasso picasso = Picasso.with(getContext()); picasso
         * .load(mDataManager.getImageUrl(goodsItem.img, mScreenWidth / 2))
         * .placeholder(R.drawable.placeholder) .into(mGoodsImage); if
         * (Strings.isNullOrEmpty(mGoodsItem.promotionImg)) {
         * mPromotionImage.setVisibility(GONE); } else {
         * mPromotionImage.setVisibility(VISIBLE); int size =
         * getResources().getDimensionPixelSize(R.dimen.promotion_image_size);
         * picasso.load(mDataManager.getImageUrl(mGoodsItem.promotionImg,
         * size)).into(mPromotionImage); }
         */

    }

    public void setGoodItemButtonListener(IDetailGoodItemSelect listener) {
        mButtonListener = listener;
    }

    public void setTop1Icon() {
        mIvTop1.setVisibility(View.VISIBLE);

    }

    public void setSoldOut() {
        mTvSoldout.setVisibility(View.VISIBLE);
        mRlBuy.setVisibility(View.GONE);
    }
}
